home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / pack / xfh132.lzh / XFH / src / gui.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  6KB  |  207 lines

  1. /* gui.h - interface to the XFH GUI control panel by Nicola Salmoria.
  2.    Adapted from example code by Nicola Salmoria.
  3.    Copyright (C) 1991, 1992, 1993 Kristian Nielsen.
  4.  
  5.    This file is part of XFH, the compressing file system handler.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            */
  20.  
  21. /* $Log:    gui.c,v $
  22.  * Revision 1.2  93/01/14  15:29:16  Kristian
  23.  * Added RCS keywords.
  24.  * 
  25.  */
  26.  
  27.   
  28. #include "CFS.h"
  29.  
  30. #include <exec/ports.h>
  31. #include <exec/semaphores.h>
  32. #include <exec/memory.h>
  33. #include <exec/execbase.h>
  34.  
  35. #include <proto/exec.h>
  36.  
  37. #include <string.h>
  38. #include <stdlib.h>
  39.  
  40. #include <dossupport.h>
  41.  
  42. #include "gui.h"
  43.  
  44. #define GUISIGMASK SIGBREAKF_CTRL_F
  45. #define MAX_PACK_MODE 100
  46.  
  47. extern struct ExecBase *SysBase;
  48.  
  49.  
  50. /* KS1.3 support hack to fix bug in AddSemaphore(). */
  51. static void MyAddSemaphore(struct SignalSemaphore *sem){
  52.   if(SysBase->LibNode.lib_Version >= 36){
  53.     debug(("Calling KS2.0 AddSemaphore().\n"));
  54.     AddSemaphore(sem);
  55.   }else{
  56.     debug(("Using KS1.3-style AddSemaphore().\n"));
  57.     sem->ss_Link.ln_Type=NT_SIGNALSEM;
  58.     InitSemaphore(sem);
  59.     Forbid();
  60.     Enqueue(&SysBase->SemaphoreList,&sem->ss_Link);
  61.     Permit();
  62.   }
  63. }
  64.  
  65. /* Lock the semaphore and return a pointer to it, or NULL if it isn't there. */
  66. /* If it can't find it, will try to create a new one; so this function will */
  67. /* fail only due to severe low memory conditions */
  68. /* When done with the list, the semaphore must be freed with ReleaseSemaphore() */
  69.  
  70. /* You shouldn't need to call this function directly, let UpdateXFHNode() do that for you */
  71. static struct XFHSemaphore *GetSemaphore(glb glob){
  72.   struct XFHSemaphore *sem;
  73.   static UBYTE xfhsemaphorename[] = XFHSEMAPHORENAME;
  74.  
  75.   Forbid();
  76.   if(!(sem = (struct XFHSemaphore *)FindSemaphore(xfhsemaphorename)) &&
  77.     (sem = AllocMem(sizeof(struct XFHSemaphore) + sizeof(XFHSEMAPHORENAME)
  78.           + 1,MEMF_CLEAR | MEMF_PUBLIC))){
  79.     debug(("Creating new semaphore.\n"));
  80.     sem->xfh_Length = sizeof(struct XFHSemaphore);
  81.     sem->xfh_Semaphore.ss_Link.ln_Name = ((UBYTE *)(sem + 1));
  82.     strcpy(sem->xfh_Semaphore.ss_Link.ln_Name,xfhsemaphorename);
  83.     NewList(&sem->xfh_DeviceList);
  84.     MyAddSemaphore((struct SignalSemaphore *)sem);
  85.   }
  86.   Permit();
  87.  
  88.   /* out of Forbid() because the semaphore is guaranteed not to be removed */
  89.   if(sem){
  90.     debug(("Locking semaphore...\n"));
  91.     ObtainSemaphore((struct SignalSemaphore *)sem);
  92.   }else{
  93.     debug(("ERROR: No memory for new semaphore!\n"));
  94.     OUTOFMEM;
  95.   }
  96.   return(sem);
  97. }
  98.  
  99. #define bool2string(b) ( (b) ? "YES" : "NO" )
  100.  
  101. static BOOL ReadConfigSem(glb glob, struct XFHNode *node){
  102.   char buf[256];
  103.   
  104.   sprintf(buf,"STEPDOWN=%s",bool2string(node->xfh_LowMemory));
  105.   set_option(glob, buf);
  106.   
  107.   if(node->xfh_AutoPack && node->xfh_PackerID){
  108.     set_option(glob, "AUTOCOMPRESS=YES");
  109.     if(node->xfh_PackMode > MAX_PACK_MODE){
  110.       sprintf(buf,"PACKMODE=%s",(char *)&node->xfh_PackerID);
  111.     }else{
  112.       sprintf(buf,"PACKMODE=%s.%ld",(char *)&node->xfh_PackerID,node->xfh_PackMode);
  113.     }
  114.     set_option(glob, buf);
  115.   }else{
  116.     set_option(glob, "AUTOCOMPRESS=NO");
  117.   }
  118.   
  119.   return TRUE;    /* Never fail. */
  120. }
  121. #undef bool2string
  122.  
  123. static BOOL SetConfigSem(glb glob, struct XFHNode *node){
  124.  
  125.   node->xfh_LowMemory = glob->stepdown;
  126.   
  127.   node->xfh_AutoPack  = glob->autocompress;
  128.  
  129.   if(glob->packmode){
  130.     strncpy((char *)&node->xfh_PackerID, glob->packmode, sizeof(ULONG));
  131.     if(glob->packmode[4] == '.'){   /* ToDo: This is rather unsafe... */
  132.       node->xfh_PackMode = atoi(&glob->packmode[5]);
  133.     }else{
  134.       node->xfh_PackMode = MAX_PACK_MODE+1;
  135.     }
  136.   }else{
  137.     node->xfh_PackerID = 0L;
  138.     node->xfh_PackMode = MAX_PACK_MODE+1;
  139.   }
  140.  
  141.   node->xfh_RootLock = c2b(glob->xrootlock);
  142.   
  143.   return TRUE;   /* Never fail. */
  144. }
  145.  
  146.  
  147. /* this prototype of function will either create a new node and add it to */
  148. /* the semaphore, or, if it already exists, read the configuration from it. */
  149. /* You will call this function:
  150. /* a) after initialization, to read or create your node */
  151. /* b) when you receive a CTRL_F, to read your node */
  152. /**/
  153. /* your main loop will look like this:
  154.   portsignal = 1L << myport->mp_SigBit;
  155.  
  156.   recsignal = Wait(portsignal | SIGBREAKF_CTRL_F);
  157.  
  158.   if (recsignal & SIGBREAKF_CTRL_F) UpdateXFHNode();
  159.   if (recsignal & portsignal)
  160.   {
  161.     while (msg = GetMsg(myport))
  162.     {
  163.       ...usual msg processing...
  164.     }
  165.   }
  166. */
  167. BOOL UpdateXFHNode(glb glob){
  168.   struct XFHSemaphore *sem;
  169.   struct XFHNode *node;
  170.   BOOL succ;
  171.   
  172.   if(sem = GetSemaphore(glob)){
  173.     if(node=(struct XFHNode *)FindName(&sem->xfh_DeviceList,glob->devname)){
  174.       debug(("Reading config from semaphore.\n"));
  175.       succ = ReadConfigSem(glob, node);
  176.     }
  177.     else if(node = AllocMem(sizeof(struct XFHNode)
  178.                    + strlen(glob->devname) + 1,MEMF_CLEAR|MEMF_PUBLIC)){
  179.       debug(("Creating new entry in semaphore.\n"));
  180.       node->xfh_Length = sizeof(struct XFHNode);
  181.       node->xfh_Node.ln_Name = (UBYTE *)(node + 1);
  182.       strcpy(node->xfh_Node.ln_Name,glob->devname);
  183.       succ = SetConfigSem(glob, node);
  184.  
  185.       node->xfh_Handler = glob->dosport;
  186.       AddTail(&sem->xfh_DeviceList,(struct Node *)node);
  187.     }else{
  188.       debug(("Unable to create XFHNode entry!!!.\n"));
  189.       OUTOFMEM;
  190.       succ = FALSE;
  191.     }
  192.     ReleaseSemaphore((struct SignalSemaphore *)sem);
  193.   }else{
  194.    debug(("Unable to obtain semaphore!!!\n"));
  195.     succ = FALSE;
  196.   }
  197.   return succ;
  198. }
  199.  
  200.  
  201. ULONG guisigmask(glb glob){
  202.    return GUISIGMASK;
  203. }
  204.  
  205.  
  206. /* End of gui.c */
  207.